package com.tb.controller;

import com.github.pagehelper.PageInfo;
import com.tb.model.*;
import com.tb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: 后台相关操作
 * @author: wangguichun
 * @create: 2018-03-21 15:52
 **/
@Controller
@RequestMapping("/backdoor")
public class BackdoorController {

    @Autowired
    private UserService userService;
    @Autowired
    private PrisonService prisonService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private IllegalityService illegalityService;

    @RequestMapping({"/", "index"})
    public String index(Model model) {
        model.addAttribute("userCount", userService.getCount());
        model.addAttribute("userInBlackHouseCount", prisonService.getCount());
        model.addAttribute("violationCount", illegalityService.getCount());
        model.addAttribute("sensitiveWordCount", sensitiveWordService.getCount());
        return "backdoor/index";
    }

    //返回userPage需要的数据
    @RequestMapping("userPage")
    @ResponseBody
    public User userPage(int userId) {
        return userService.getUserById(userId);
    }

    //userPage转让超级管理员
    @RequestMapping("userPage/transfer")
    public String userPageTransfer(int myId, int toId) {
        userService.transferSuperAdmin(myId, toId);
        return "redirect:../../toLogin";
    }

    //userPage设置管理员
    @RequestMapping("userPage/setManager")
    public String userPageSetManager(int userId) {
        userService.setManager(userId);
        return "redirect:../userManager/1";
    }

    //userPage撤职到普通用户
    @RequestMapping("userPage/setUser")
    public String userPageSetUser(int userId) {
        userService.setUser(userId);
        return "redirect:../userManager/1";
    }

    //userPage删除用户
    @RequestMapping("userPage/deleteUser")
    public String userPageDeleteUser(int userId) {
        userService.deleteUser(userId);
        return "redirect:../userManager/1";
    }

    //userPage激活用户
    @RequestMapping("userPage/activateUser")
    public String userPageActivateUser(int userId) {
        userService.activateUser(userId);
        return "redirect:../userManager/1";
    }

    //userPage关小黑屋
    @RequestMapping("userPage/sendBlackHouse")
    public String userPageSendBlackHouse(int userId, int hour) {
        prisonService.addUser(userId, hour);
        return "redirect:../blackHouse/1";
    }


    @RequestMapping("userManager")
    public String userManagerDefault() {
        return "redirect:userManager/1";
    }

    @RequestMapping("userManager/{pageid}")
    public String userManager(Model model, @PathVariable Integer pageid) {

//        int pageCount = userService.getCount() / 15 + 1;
        PageInfo<User> pageInfo = userService.getUserListByPage(pageid);
//        PageBean<User> pageBean = new PageBean<>(pageCount, pageid);
//        pageBean.setList(userList);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);

        return "backdoor/userManager";
    }

    @RequestMapping("illegalities")
    public String illegalitiesDefault() {
        return "redirect:illegalities/1";
    }

    @RequestMapping("illegalities/{pageId}")
    public String illegalities(Model model, @PathVariable Integer pageId) {
//        int pageCount = illegalityService.getCount() / 16 + 1;
        PageInfo<Illegality> pageInfo = illegalityService.getListByPageId(pageId);
//        PageBean<Illegality> pageBean = new PageBean<>(pageCount, pageId);
//        pageBean.setList(list);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "backdoor/illegalities";
    }

    @RequestMapping("illegalities/delete/{id}")
    public String illegalitiesDelete(@PathVariable Integer id) {
        illegalityService.deleteIllegalityById(id);
        return "redirect:../1";
    }

    @RequestMapping("illegalities/ignore/{id}")
    public String illegalitiesIgnore(@PathVariable Integer id) {
        illegalityService.ignoreIllegalityById(id);
        return "redirect:../1";
    }

    @RequestMapping("blackHouse")
    public String blackHouseDefault() {
        return "redirect:blackHouse/1";
    }

    @RequestMapping("blackHouse/{pageid}")
    public String blackHouse(Model model, @PathVariable Integer pageid) {
//        int pageCount = prisonService.getCount() / 15 + 1;
        PageInfo<Prison> pageInfo = prisonService.getUserListByPage(pageid);
//        PageBean<Prison> pageBean = new PageBean<>(pageCount, pageid);
//        pageBean.setList(prisonList);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "backdoor/blackHouse";
    }

    @RequestMapping("blackHouse/delete/{userId}")
    public String blackHouseDeleteByUserID(@PathVariable Integer userId) {
        prisonService.deleteUserByUserId(userId);
        return "redirect:../1";
    }

    @RequestMapping("topicManager")
    @SuppressWarnings(value = "unchecked")
    public String topicManager(Model model) {
//        PageBean<Topic> pageBean = new PageBean<>(1, 1);
//        pageBean.setList(topicService.getTopicList());
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("topicList", topicService.getTopicList());
        return "backdoor/topicManager";
    }

    @RequestMapping("topicManager/delete/{topicId}")
    public String topicManagerDelete(@PathVariable Integer topicId) {
        topicService.deleteTopicById(topicId);
        return "redirect:../../topicManager";
    }

    @RequestMapping("topicManager/add/{word}")
    public String topicManagerAdd(@PathVariable String word) {
        topicService.addTopic(word);
        return "redirect:../../topicManager";
    }

    @RequestMapping("wordFilter")
    public String wordFilter(Model model) {
//        PageBean<SensitiveWord> pageBean = new PageBean<>(1, 1);
//        pageBean.setList(sensitiveWordService.getWordList());
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("wordList", sensitiveWordService.getWordList());
        return "backdoor/wordFilter";
    }

    @RequestMapping("wordFilter/delete/{id}")
    public String wordFilterDelete(@PathVariable Integer id) {
        sensitiveWordService.deleteById(id);
        return "redirect:../../wordFilter";
    }

    @RequestMapping("wordFilter/add/{word}")
    public String wordFilterAdd(@PathVariable String word) {
        sensitiveWordService.addWord(word);
        return "redirect:../../wordFilter";
    }

    @RequestMapping("calendar")
    public String calendar() {
        return "backdoor/calendar";
    }

}
